package com.chinasoft.ssm.controller;

import com.chinasoft.ssm.pojo.Admin;
import com.chinasoft.ssm.pojo.Result;
import com.chinasoft.ssm.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@Slf4j
public class AdminController {
    @Autowired
    AdminService adminService;

    @PostMapping("/Aupload")
    public String UploadPicture(@RequestParam("file") MultipartFile file, RedirectAttributes attributes) throws IOException {

        // 上传文件/图像到指定文件夹（这里可以改成你想存放地址的相对路径）
        File savePos = new File("vue-ssgl/vue-project/src/assets");
        if(!savePos.exists()){  // 不存在，则创建该文件夹
            savePos.mkdir();
            log.info("555");
        }
        log.info(savePos.toString());
        // 获取存放位置的规范路径
        String realPath = savePos.getCanonicalPath();
        log.info("222");
        // 上传该文件/图像至该文件夹下
        file.transferTo(new File(realPath+"/"+file.getOriginalFilename()));
        log.info(file.getOriginalFilename());
        log.info("33");
        attributes.addFlashAttribute("message","添加成功！");
        log.info("2224");
        return "redirect:vue-ssgl/vue-project/src/components/Abike";
    }
    @PostMapping("/Alog")
    public Result login(@RequestBody Admin admin) {
        Result result = new Result();
        Admin a = adminService.login1(admin.getName());
        log.info(admin.getName() + "  "+admin.getPassword());
        if(a.getPassword().equals(admin.getPassword())) {
            result.setData(a);
            result.setMessage("登录成功");
            result.setStatus(200);
            log.info("调用成功");
        }else{
            result.setData(null);
            result.setMessage("登录失败");
            result.setStatus(404);
            log.info("调用成功");
        }
        return result;
    }

    @GetMapping("/AlogONE/{name}")
    public Admin all(@PathVariable String name) {
        return adminService.selectone(name);
    }

    @GetMapping("/adminId/{id}")
    public Result getOne(@PathVariable int id) {
        Result result = new Result();
        Admin a = adminService.selectById(id);
        if(a!=null) {
            result.setData(a);
            result.setMessage("登录成功");
            result.setStatus(200);
        }else{
            result.setData(null);
            result.setMessage("登录失败");
            result.setStatus(404);
        }
        log.info("调用成功");
        return result;

    }

    @PostMapping("/Aupd")
    public Result update(@RequestBody Admin admin) {
        Result result = new Result();
        Integer data = adminService.update(admin);
        result.setData(data);
        result.setMessage("修改成功");
        result.setStatus(200);
        log.info("修改成功");
        return result;
    }
}
